/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.parties.web;

import static au.org.aekos.ands.global.Util.serialise;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import au.org.aekos.ands.global.Schema;
import au.org.ands.standards.rifCs.registryObjects.RegistryObjectsDocument.RegistryObjects.RegistryObject;

import com.google.common.base.Splitter;

/**
 * The controller for REST-oriented provision of party information.
 * This class does not interface with any external databases; it merely
 * pulls information from the supplied metadata properties.
 */
@RequestMapping("/")
@Controller
public class Parties {
	private static final String PROPERTIES_KEY_PARTIES = "parties";
	
	private Schema schema;
	private Map<String,RegistryObject> partyMap = new HashMap<String,RegistryObject>();

	/**
	 * Instantiates a new controller for loading party information from metadata properties
	 * located on the classpath.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public Parties() throws IOException {
		this(Parties.class.getClassLoader().getResourceAsStream("metadata.properties"));
	}
	
	/**
	 * Instantiates a new controller for loading party information from metadata properties
	 * provided through an input stream.
	 *
	 * @param properties the stream through which properties will be provided.
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public Parties(InputStream properties) throws IOException {
		schema = new Schema(properties);	
		loadParties();
	}
	
	/**
	 * Populates the internal party map.
	 */
	private void loadParties() {
		String partyListProperty = schema.prop(PROPERTIES_KEY_PARTIES);
		if (partyListProperty == null) { return; }
		
		for (String partyID : Splitter.on(" ").omitEmptyStrings().trimResults().split(partyListProperty)) {
			partyMap.put(partyID, schema.generateParty(partyID));
		}
	}
    
	// No Activities or Collections
	
    // Party
    
    /**
	 * Returns the set of party identifiers.
	 *
	 * @return the set.
	 */
	@RequestMapping(value = "/parties", method = RequestMethod.GET)
    public @ResponseBody Set<String> listParties() {
    	return partyMap.keySet();
    }

    /**
     * Returns the party identified by the given string.
     *
     * @param id the party identifier.
     * @return the party (as a RIF-CS XML string), if one corresponds to that identifier, otherwise null.
     */
    @RequestMapping(value = "/party/{id:.*}", method = RequestMethod.GET)
    public @ResponseBody String getParty(@PathVariable String id) {
    	return serialise(partyMap.get(id));
    }
}
